package com.orangelife.mobile.goodsrent.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;

import java.util.List;
import java.util.Map;

public class GoodsRentOrderDetailListAdapter<T> extends FatherAdapter<T> {
	private Context context;
	private List<T> list;
	private Map<String, Object> map;
	private ImageDownloader image = ImageDownloader.getImageDownloader();

	public GoodsRentOrderDetailListAdapter(Context context, List<T> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@SuppressWarnings("unchecked")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_goodsrent_order_detaill, null);
			viewHolder.ivGoods = (ImageView) convertView
					.findViewById(R.id.ivGoods);
			viewHolder.tvGoodsName = (TextView) convertView
					.findViewById(R.id.tvGoodsName);
			viewHolder.tvMarkerPrice = (TextView) convertView
					.findViewById(R.id.tvMarkerPrice);
			viewHolder.tvUnitPrice = (TextView) convertView
					.findViewById(R.id.tvUnitPrice);
			viewHolder.tvTotalNumber = (TextView) convertView
					.findViewById(R.id.tvTotalNumber);
			viewHolder.tvSpecsName = (TextView) convertView
					.findViewById(R.id.tvSpecsName);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		map = (Map<String, Object>) list.get(position);
//		Map<String ,Object> snapMap = (Map<String, Object>) map.get("snap");
		Map<String ,Object> snapMap = JSONHelper.jsonToMap(map.get("snap").toString());
		image.download(snapMap.get("goodsLogo").toString(), viewHolder.ivGoods, ImageDownloader.ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
		viewHolder.tvGoodsName.setText(snapMap.get("goodsName").toString());
//		viewHolder.tvSpecsName.setText("");
		viewHolder.tvTotalNumber.setText("天数X"+map.get("rentalDays").toString());
		viewHolder.tvUnitPrice.setText("¥"+snapMap.get("price").toString());
		viewHolder.tvMarkerPrice.setText("¥"+snapMap.get("price").toString());
		viewHolder.tvMarkerPrice.getPaint().setFlags(
				Paint.STRIKE_THRU_TEXT_FLAG);
		viewHolder.tvMarkerPrice.setVisibility(View.GONE);

		return convertView;
	}

	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = list;
		return GoodsRentOrderDetailListAdapter.this;
	}

	class ViewHolder {
		/** 团购和商品 */
		private ImageView ivGoods;
		private TextView tvGoodsName;
		private TextView tvSpecsName;
		private TextView tvTotalNumber;
		private TextView tvUnitPrice;
		private TextView tvMarkerPrice;
	}

}
